cmake_minimum_required(VERSION 3.10.2)

###########################################################
#### Project configuration                             ####
###########################################################

project(MMMViewer)

###########################################################
#### Source configuration                              ####
###########################################################

set(SOURCE_FILES
    MMMViewerMain.cpp
    MMMViewerWindow.cpp
    PluginHandler/PluginHandlerDialog.cpp
    SaveMotionDialog.cpp
    SensorVisualisation/SensorVisualisationHandler.cpp
    SensorVisualisation/SensorVisualisationTable/SensorVisualisationWidgetItem.cpp
    SensorVisualisation/SensorVisualisationTable/SensorVisualisationTable.cpp
    ../common/ApplicationBaseConfiguration.cpp
    ../common/MotionListWidget.cpp
    ../common/MotionListConfiguration.cpp
    ../common/HandleMotionsWithoutModel.cpp
)

set(HEADER_FILES
    MMMViewerWindow.h
    PluginHandler/PluginHandler.h
    PluginHandler/PluginHandlerDialog.h
    SaveMotionDialog.h
    MotionHandler.h
    MotionHandlerFactory.h
    SensorVisualisation/SensorVisualisationFactory.h
    SensorVisualisation/SensorVisualisation.h
    SensorVisualisation/SensorVisualisationHandler.h
    SensorVisualisation/SensorVisualisationTable/SensorVisualisationWidgetItem.h
    SensorVisualisation/SensorVisualisationTable/SensorVisualisationTable.h
    ../common/ApplicationBaseConfiguration.h
    ../common/MotionListWidget.h
    ../common/MotionListConfiguration.h
    ../common/HandleMotionsWithoutModel.h
)

set(MOC_FILES
    MMMViewerWindow.h
    SaveMotionDialog.h
    MotionHandler.h
    PluginHandler/PluginHandlerDialog.h
    SensorVisualisation/SensorVisualisationHandler.h
    SensorVisualisation/SensorVisualisationTable/SensorVisualisationTable.h
    ../common/MotionListWidget.h
)

set(UI_FILES
    PluginHandler/PluginHandlerDialog.ui
    MMMViewerWindow.ui
    SaveMotionDialog.ui
)

###########################################################
#### CMake package configuration                       ####
###########################################################

find_package(MMMCore REQUIRED)
set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS})

find_package(Simox REQUIRED)
set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS} VirtualRobot)

set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS} MMMSimoxTools)

###########################################################
#### Project build configuration                       ####
###########################################################

# create the executable
SimoxQtApplication(${PROJECT_NAME} "${SOURCE_FILES}" "${HEADER_FILES}" "${MOC_FILES}" "${UI_FILES}")
target_link_libraries(${PROJECT_NAME} PRIVATE ${EXTERNAL_LIBRARY_DIRS} dl)

#GET_FILENAME_COMPONENT (CurrentPath ${CMAKE_CURRENT_LIST_FILE} PATH)

set(MMMViewer_BASE_DIR ${PROJECT_SOURCE_DIR} CACHE INTERNAL "" )
add_definitions(-DMMMViewer_BASE_DIR="${MMMViewer_BASE_DIR}" -D_SCL_SECURE_NO_WARNINGS)

install(
    TARGETS ${PROJECT_NAME}
    EXPORT "${CMAKE_PROJECT_NAME}Targets"
    #LIBRARY DESTINATION lib
    #ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    #INCLUDES DESTINATION include
    COMPONENT bin
)

add_definitions(-DMMMTools_LIB_DIR="${MMMTools_LIB_DIR}")
add_definitions(-DMMMTools_SRC_DIR="${MMMTools_SOURCE_DIR}")
add_definitions(-DSENSOR_VISUALISATION_PLUGIN_LIB_DIR="${SENSOR_VISUALISATION_PLUGIN_LIB_DIR}")
add_definitions(-DMOTION_HANDLER_PLUGIN_LIB_DIR="${MOTION_HANDLER_PLUGIN_LIB_DIR}")

###########################################################
#### Compiler configuration                            ####
###########################################################

include_directories(${EXTERNAL_INCLUDE_DIRS})
link_directories(${EXTERNAL_LIBRARY_DIRS})
add_definitions(${EXTERNAL_LIBRARY_FLAGS})

###########################################################
#### Sensor Visualisation Plugins                      ####
###########################################################

# Add sensor visualisation plugins
function(DefineMotionSensorVisualisationPlugin SensorVisualisationPlugin_Name SensorVisualisationPlugin_Sources SensorVisualisationPlugin_Headers SensorPlugin_Name)
    add_library(${SensorVisualisationPlugin_Name} SHARED ${SensorVisualisationPlugin_Sources} ${SensorVisualisationPlugin_Headers})
    target_link_libraries(${SensorVisualisationPlugin_Name} PUBLIC ${EXTERNAL_LIBRARY_DIRS} ${SensorPlugin_Name})
    set_target_properties(${SensorVisualisationPlugin_Name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${SENSOR_VISUALISATION_PLUGIN_LIB_DIR})

    install(
        TARGETS ${SensorVisualisationPlugin_Name}
        EXPORT "${CMAKE_PROJECT_NAME}Targets"
        LIBRARY DESTINATION lib/mmm_plugins/sensor_visualisation
        #ARCHIVE DESTINATION lib
        #RUNTIME DESTINATION bin
        #INCLUDES DESTINATION include
        COMPONENT lib
    )
    install(FILES ${SensorVisualisationPlugin_Headers} DESTINATION "include/MMM/${SensorVisualisationPlugin_Name}" COMPONENT dev)
endfunction()

add_subdirectory(SensorVisualisation/SensorVisualisationPlugin)

###########################################################
#### Motion Handler Plugin                             ####
###########################################################

add_subdirectory(MotionHandlerPlugin)
